/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import beans.item;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author appi
 */
@WebServlet(name = "PurchaseActn", urlPatterns = {"/PurchaseActn.do"})
public class PurchaseActn extends HttpServlet {

    private Object ArrayList;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           int qty2=0;
            boolean flag = false;
                int id = Integer.parseInt(request.getParameter("id"));
               String qty1= request.getParameter("qty");
               if(!qty1.equalsIgnoreCase("")){
                   qty2 = Integer.parseInt(qty1);
               }
              
            
            HttpSession ssn = request.getSession();
            ArrayList<item> allitem = (ArrayList<item>) ssn.getAttribute("product");
            ArrayList<item> cart = (ArrayList<item>) ssn.getAttribute("cart");
            
            /*here u get n complexity, if u take loop inside loop then u get n^2
             which is not good approach */
            item i1=new item();
            i1.setIqty(qty2);
            String msg=i1.verifQty();
            //if(msg.equals("")){
            for (item i3 : cart) {

                if (id == i3.getId()) {
                    //item i4 = new item();

                    /*here we checked that is id in cart? if yes, it mean u hv already entered
                     quantity one time, if u want to add more quantity then just add in previosly
                     entered quantity*/

                    qty1 = qty1 + i3.getIqty();
                    i3.setIqty(qty2);
                    flag = true;
                }
            }

            if (flag == false) {
                for (item i : allitem) {
                    if (id == i.getId()) {
//                    item i2 = new item();

                        /* here we have to create new object of item because if we don't, 
                         then last entered quantity will be considered but if u create one new 
                         object after maching the id then next time considers those object for
                         quantity and total nomber of quantity will be given*/

//                 i2.setId(i.getId());
//                    i2.setIname(i.getIname());
//                    i2.setIqty(qty1);
//                    i2.setIprice(i.getIprice());
//                    i2.setIsdesc(i.getIsdesc());
                        i.setIqty(qty2);
                        cart.add(i);

                    }
                }
                //}

                //i.setIqty(qty1);
                //get cart from session
                // ArrayList<item> cart = (ArrayList<item>) ssn.getAttribute("cart");
                // cart.add(i2);

                ssn.setAttribute("cart", cart);
                response.sendRedirect("home.jsp");

            }
        // }
            else
            {
                HttpSession sn = request.getSession();
                sn.setAttribute("msg", msg);
                response.sendRedirect("ProductDetail.jsp");
            }
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
